home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / source / graphicgems4.lha / GemsIV / nurb_polyg / GraphicsGems.h < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-06  |  5.0 KB  |  185 lines

  1. /*
  2.  * GraphicsGems.h
  3.  * Version 1.0 - Andrew Glassner
  4.  * from "Graphics Gems", Academic Press, 1990
  5.  */
  6.  
  7. #ifndef GG_H
  8.  
  9. #define GG_H 1
  10.  
  11. /*********************/
  12. /* 2d geometry types */
  13. /*********************/
  14.  
  15. typedef struct Point2Struct {    /* 2d point */
  16.     double x, y;
  17.     } Point2;
  18. typedef Point2 Vector2;
  19.  
  20. typedef struct IntPoint2Struct {    /* 2d integer point */
  21.     int x, y;
  22.     } IntPoint2;
  23.  
  24. typedef struct Matrix3Struct {    /* 3-by-3 matrix */
  25.     double element[3][3];
  26.     } Matrix3;
  27.  
  28. typedef struct Box2dStruct {        /* 2d box */
  29.     Point2 min, max;
  30.     } Box2;
  31.  
  32.  
  33. /*********************/
  34. /* 3d geometry types */
  35. /*********************/
  36.  
  37. typedef struct Point3Struct {    /* 3d point */
  38.     double x, y, z;
  39.     } Point3;
  40. typedef Point3 Vector3;
  41.  
  42. typedef struct IntPoint3Struct {    /* 3d integer point */
  43.     int x, y, z;
  44.     } IntPoint3;
  45.  
  46.  
  47. typedef struct Matrix4Struct {    /* 4-by-4 matrix */
  48.     double element[4][4];
  49.     } Matrix4;
  50.  
  51. typedef struct Box3dStruct {        /* 3d box */
  52.     Point3 min, max;
  53.     } Box3;
  54.  
  55.  
  56.  
  57. /***********************/
  58. /* one-argument macros */
  59. /***********************/
  60.  
  61. /* absolute value of a */
  62. #define ABS(a)        (((a)<0) ? -(a) : (a))
  63.  
  64. /* round a to nearest integer towards 0 */
  65. #define FLOOR(a)    ((a)>0 ? (int)(a) : -(int)(-a))
  66.  
  67. /* round a to nearest integer away from 0 */
  68. #define CEILING(a) \
  69. ((a)==(int)(a) ? (a) : (a)>0 ? 1+(int)(a) : -(1+(int)(-a)))
  70.  
  71. /* round a to nearest int */
  72. #define ROUND(a)    ((a)>0 ? (int)(a+0.5) : -(int)(0.5-a))
  73.  
  74. /* take sign of a, either -1, 0, or 1 */
  75. #define ZSGN(a)        (((a)<0) ? -1 : (a)>0 ? 1 : 0)
  76.  
  77. /* take binary sign of a, either -1, or 1 if >= 0 */
  78. #define SGN(a)        (((a)<0) ? -1 : 0)
  79.  
  80. /* shout if something that should be true isn't */
  81. #define ASSERT(x) \
  82. if (!(x)) fprintf(stderr," Assert failed: x\n");
  83.  
  84. /* square a */
  85. #define SQR(a)        ((a)*(a))
  86.  
  87.  
  88. /***********************/
  89. /* two-argument macros */
  90. /***********************/
  91.  
  92. /* find minimum of a and b */
  93. #define MIN(a,b)    (((a)<(b))?(a):(b))
  94.  
  95. /* find maximum of a and b */
  96. #define MAX(a,b)    (((a)>(b))?(a):(b))
  97.  
  98. /* swap a and b (see Gem by Wyvill) */
  99. #define SWAP(a,b)   { a^=b; b^=a; a^=b; }
  100.  
  101. /* linear interpolation from l (when a=0) to h (when a=1)*/
  102. /* (equal to (a*h)+((1-a)*l) */
  103. #define LERP(a,l,h) ((l)+(((h)-(l))*(a)))
  104.  
  105. /* clamp the input to the specified range */
  106. #define CLAMP(v,l,h)    ((v)<(l) ? (l) : (v) > (h) ? (h) : v)
  107.  
  108.  
  109. /****************************/
  110. /* memory allocation macros */
  111. /****************************/
  112.  
  113. /* create a new instance of a structure (see Gem by Hultquist) */
  114. #define NEWSTRUCT(x)    (struct x *)(malloc((unsigned)sizeof(struct x)))
  115.  
  116. /* create a new instance of a type */
  117. #define NEWTYPE(x)  (x *)(malloc((unsigned)sizeof(x)))
  118.  
  119.  
  120. /********************/
  121. /* useful constants */
  122. /********************/
  123.  
  124. #define PI    3.141592    /* the venerable pi */
  125. #define PITIMES2    6.283185    /* 2 * pi */
  126. #define PIOVER2        1.570796    /* pi / 2 */
  127. #define E    2.718282    /* the venerable e */
  128. #define SQRT2        1.414214    /* sqrt(2) */
  129. #define SQRT3        1.732051    /* sqrt(3) */
  130. #define GOLDEN        1.618034    /* the golden ratio */
  131. #define DTOR        0.017453    /* convert degrees to radians */
  132. #define RTOD        57.29578    /* convert radians to degrees */
  133.  
  134.  
  135. /************/
  136. /* booleans */
  137. /************/
  138.  
  139. #define TRUE        1
  140. #define FALSE        0
  141. #define ON    1
  142. #define OFF        0
  143. typedef int boolean;        /* boolean data type */
  144. typedef boolean flag;        /* flag data type */
  145.  
  146.  
  147. /******************/
  148. /*   3d Library      */
  149. /******************/
  150.  
  151. /* returns squared length of input vector */
  152. extern double V3SquaredLength(Vector3 *);
  153. /* returns length of input vector */
  154. extern double V3Length(Vector3 *);
  155. /* negates the input vector and returns it */
  156. extern Vector3 *V3Negate(Vector3 *);
  157. /* normalizes the input vector and returns it */
  158. extern Vector3 *V3Normalize(Vector3 *);
  159. /* scales the input vector to the new length and returns it */
  160. extern Vector3 *V3Scale(Vector3 *, double );
  161. /* return vector sum c = a+b */
  162. extern Vector3 *V3Add(Vector3 *, Vector3 *, Vector3 *);
  163. /* return vector difference c = a-b */
  164. extern Vector3 *V3Sub(Vector3 *, Vector3 *, Vector3 *);
  165. /* return the dot product of vectors a and b */
  166. extern double V3Dot(Vector3 *, Vector3 *);
  167. /* linearly interpolate between vectors by an amount alpha */
  168. /* and return the resulting vector. */
  169. /* When alpha=0, result=lo.  When alpha=1, result=hi. */
  170. extern Vector3 *V3Lerp(Vector3 *, Vector3 *, double , Vector3 *);
  171. /* make a linear combination of two vectors and return the result. */
  172. /* result = (a * ascl); + (b * bscl); */
  173. extern Vector3 *V3Combine (Vector3 *, Vector3 *, Vector3 *, double , double);
  174. /* multiply two vectors together component-wise and return the result */
  175. extern Vector3 *V3Mul (Vector3 *, Vector3 *, Vector3 *);
  176. /* return the distance between two points */
  177. extern double V3DistanceBetween2Points(Point3 *, Point3 * );
  178. /* return the cross product c = a cross b */
  179. extern Vector3 *V3Cross(Vector3 *a, Vector3 *, Vector3 *);
  180. /* create, initialize, and return a new vector */
  181. extern Vector3 *V3New(double, double, double);
  182. /* create, initialize, and return a duplicate vector */
  183. extern Vector3 *V3Duplicate(Vector3 *);
  184. #endif
  185.